package com.aigame.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.aigame.entity.Order;
import com.aigame.util.DBHelper;

public class OrderDao {
	private DBHelper dbHelper;
	private Order order = null;

	public OrderDao(Context c) {
		this.dbHelper = new DBHelper(c, "ConsumeOrder");
	}

	public void recordOrder(Order order) {
		try{
			ContentValues values = new ContentValues();
			values.put("appid", order.getAppid());
			values.put("serverid", order.getServerid());
			values.put("orderid", order.getOrderid());
			values.put("uid", order.getUid());
			values.put("money", order.getMoney());
			values.put("productid", order.getProductid());
			values.put("productname", order.getProductname());
			values.put("orderstate", "0");
			if (dbHelper.queryBy("orderid", order.getOrderid()).getCount() == 0) {
				dbHelper.insert(values);
			}
		}finally {
			dbHelper.close();
		}
	}

	public Boolean finishOrder(Order order) {
		try {
			Cursor c = dbHelper.queryBy("orderid", order.getOrderid());
			if (c.getCount() > 0) {
				ContentValues values = new ContentValues();
				values.put("orderstate", "1");
				dbHelper.update(values, "orderid", order.getOrderid());
				return true;
			}
			return false;
		} finally {
			dbHelper.close();
		}
	}

	public Order queryOrder(Order order) {
		try {

			Cursor c = dbHelper.queryBy("orderid", order.getOrderid());
			if (c.getCount() > 0) {
				c.moveToFirst();
				String appid = c.getString(c.getColumnIndex("appid"));
				String serverid = c.getString(c.getColumnIndex("serverid"));
				String orderid = c.getString(c.getColumnIndex("orderid"));
				String uid = c.getString(c.getColumnIndex("uid"));
				String money = c.getString(c.getColumnIndex("money"));
				String productid = c.getString(c.getColumnIndex("productid"));
				String productname = c.getString(c
						.getColumnIndex("productname"));
				String orderstate = c.getString(c.getColumnIndex("orderstate"));
				this.order = new Order();
				this.order.setAppid(appid);
				this.order.setServerid(serverid);
				this.order.setOrderid(orderid);
				this.order.setUid(uid);
				this.order.setMoney(money);
				this.order.setProductid(productid);
				this.order.setProductname(productname);
				this.order.setOrderstate(orderstate);
				return this.order;
			}
			return null;
		}finally {
			dbHelper.close();
		}
		
	}

}
